package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import entity.Order;
import util.db.DBhelp;
import util.db.Page;
import util.db.RowMapper;

public class OrderDao {

	DBhelp<Order> dBhelp = new DBhelp<Order>();
	
	
	
	
	
	
	private class OrderRowMapper implements RowMapper<Order>{

		public Order mapRow(ResultSet resultSet) throws SQLException {
			Order order = new Order();
			order.setId(resultSet.getString("id"));
			order.setName(resultSet.getString("name"));
			order.setAddress(resultSet.getString("address"));
			order.setTel(resultSet.getString("tel"));
			order.setContent(resultSet.getString("content"));
			order.setPaytype(resultSet.getString("paytype"));
			order.setPay(resultSet.getString("pay"));
			order.setTotal(resultSet.getFloat("total"));
			order.setCreatetime(resultSet.getString("createtime"));
			order.setUserid(resultSet.getString("userid"));
			order.setState(resultSet.getString("state"));
			return order;
		}
		
	}



	public void save(Order order) {
		String sql = "insert into t_order (id,name,address,tel,content,paytype,pay,total,userid,state)values(?,?,?,?,?,?,?,?,?,?)";
		dBhelp.update(sql, order.getId(),order.getName(),order.getAddress(),order.getTel(),order.getContent(),order.getPaytype(),order.getPay(),order.getTotal(),order.getUserid(),order.getState());
		
	}

	public int countByUserid(String userid){
		String sql = "select count(*) from t_order where userid=?";
		return dBhelp.executeQuery(sql, userid).intValue();
	}

	public Page<Order> findAllOrderByUserIdAndNowPageNum(String id,
			int nowPageNum) {
		Page<Order> page = new Page<Order>(nowPageNum, countByUserid(id));
		String sql = "SELECT id,name,address,tel,content,paytype,pay,total,userid,state,createtime FROM t_order where userid=? order by createtime desc limit ?,?";
		List<Order> list = dBhelp.executeQueryForList(sql, new OrderRowMapper(), id,page.getStartPage(),page.getPagesize());
		page.setList(list);
		return page;
	}

	public Order findOrderByOrderid(String orderid) {
		String sql = "select * from t_order where id=?";
		return dBhelp.executeQueryToObject(sql, new OrderRowMapper(), orderid);
	}

	public int countAllOrder(){
		String sql = "select count(*) from t_order";
		return dBhelp.executeQuery(sql).intValue();
	}
	
	public Page<Order> findAllOrder(int nowPageNum) {
		Page<Order> page = new Page<Order>(nowPageNum, countAllOrder());
		String sql = "SELECT id,name,address,tel,content,paytype,pay,total,userid,state,createtime FROM t_order order by createtime desc limit ?,?";
		List<Order> list = dBhelp.executeQueryForList(sql, new OrderRowMapper(),page.getStartPage(),page.getPagesize());
		page.setList(list);
		return page;
		
	}

	
	
	public Page<Order> findOrderByTime(String orderaccesstime, int nowpagenum) {
		Page<Order> page = new Page<Order>(nowpagenum, countByOrderTime(orderaccesstime));
		String sql = "SELECT id,name,address,tel,content,paytype,pay,total,userid,state,createtime FROM t_order where createtime > ? order by createtime desc limit ?,?";
		List<Order> list = dBhelp.executeQueryForList(sql, new OrderRowMapper(), orderaccesstime,page.getStartPage(),page.getPagesize());
		page.setList(list);
		return page;
	}

	public int countByOrderTime(String orderaccesstime) {
		String sql = "select count(*) from t_order where createtime > ?";
		return dBhelp.executeQuery(sql, orderaccesstime).intValue();
	}

	public List<Order> findallByUserId(String id) {
		String sql = "select * from t_order where userid = ? order by createtime desc";
		return dBhelp.executeQueryForList(sql, new OrderRowMapper(), id);
	}

	public void update(Order order) {
		
		String sql = "update t_order set tel=?,content=?,paytype=?,pay=?,userid=?,state=? where id=?";
		dBhelp.update(sql, order.getTel(),order.getContent(),order.getPaytype(),order.getPay(),order.getUserid(),order.getState(),order.getId());
	}

	
	
}
